package com.sfac.day03.config;

import com.sfac.day03.interceptor.RequestViewMappingInterceptor;
import com.sfac.day03.modules.common.entity.ResourceConfigBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@AutoConfigureAfter({ WebMvcConfigurer.class})
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    ResourceConfigBean resourceConfigBean;
    @Autowired
    private RequestViewMappingInterceptor requestViewMappingInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
            registry.addResourceHandler(resourceConfigBean.getPattern())
                    .addResourceLocations(ResourceUtils.FILE_URL_PREFIX + resourceConfigBean.getWindows());
        } else {
            registry.addResourceHandler(resourceConfigBean.getPattern())
                    .addResourceLocations(ResourceUtils.FILE_URL_PREFIX + resourceConfigBean.getLinux());
        }
    }

    /**
     * - 注册拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(requestViewMappingInterceptor).addPathPatterns("/**");
        WebMvcConfigurer.super.addInterceptors(registry);

    }
}
